const sidebarCommand = 'viewFdSidebar';

var foxydict = 
{
  historyCount: null,
  autoLogin: false,
  fdHistory: [],

  dump: function(msg)
  {
    dump('FoxyDict: ' + msg + '\n');
  },

  add2history: function(word)
  {
    if (foxydict.fdHistory.indexOf(word) != -1)
       {
         var windex = foxydict.fdHistory.indexOf(word);
         foxydict.fdHistory = foxydict.fdHistory.filter( function (element, index, array) {
	 						   return index != windex;
							 });
       }
       else
       {
         if (foxydict.fdHistory.length >= foxydict.historyCount)
           foxydict.fdHistory.shift();
       }
       foxydict.fdHistory.push(word);
  },

  toggleSbPinyin: function()
  {
    let prefs = Components.classes['@mozilla.org/preferences-service;1']
			  .getService(Components.interfaces.nsIPrefService).getBranch('extensions.foxydict.');
    let pref_sbpy = prefs.getBoolPref('statusbarpinyin');
    prefs.setBoolPref('statusbarpinyin', !pref_sbpy);
  },

  onload: function (event)
  {
    foxydict.dump("onLoad\n");
    foxydictPrefObserver.register();
    let prefs = Components.classes["@mozilla.org/preferences-service;1"]
    			  .getService(Components.interfaces.nsIPrefService).getBranch("extensions.foxydict.");
    foxydict.historyCount = prefs.getIntPref("historycount"); 
    foxydict.autoLogin = prefs.getBoolPref("autologin");
    if (foxydict.autoLogin) { /* TODO: login here */ }
  },

  lookItUp: function (event)
  {
     try
     {
       let word = window.content.getSelection().toString();
       foxydict.add2history(word);
       
       let broadcaster = document.getElementById(sidebarCommand);
       if (!broadcaster.hasAttribute("checked"))
         toggleSidebar(sidebarCommand, true);
       else
	 document.getElementById('sidebar').contentWindow.pageOnLoad();
     } catch (e)
     { foxydict.dump('Exception: ' + e); }
  },
};

function PrefObserver(branchName, func)
{
  var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefService);
  var branch = prefs.getBranch(branchName);
  branch.QueryInterface(Components.interfaces.nsIPrefBranch2);

  this.register = function ()
  {
    branch.addObserver('', this, false);
    branch.getChildList('', {})
    	  .forEach( function (name) { func(branch, name); } );
  };

  this.unregister = function()
  {
    if (branch)
      branch.removeObserver('', this);
  };

  this.observe = function(aSubject, aTopic, Data)
  {
    if (aTopic == 'nsPref:changed')
      func(branch, Data);
  };
}

var foxydictPrefObserver = new PrefObserver('extensions.foxydict.',
					     function(branch, name)
					     {
					       if (name == 'statusbarpinyin')
					       {
					         let icon = branch.getBoolPref(name);
//						 icon = icon.getBoolPref('statusbarpinyin');
						 if (icon)
						 {
						   document.getElementById('sbimage').src = 'chrome://foxydict/skin/stardict16.png';
						   document.getElementById('pinyin').disabled = false;
						 }
						 else
						 {
						   document.getElementById('sbimage').src = 'chrome://foxydict/skin/stardict16_grey.png';
						   document.getElementById('pinyin').disabled = true;
						 }
					       }
					     });

window.addEventListener("load", foxydict.onload, false);
